package events.network.riteh;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import events.network.riteh.R;
import events.network.riteh.RequestMethod;
import android.app.AlertDialog;

public class EventNetworkActivity extends Activity {

	RestClient client;
	Button mLogin;
	EditText mEmail, mPassword;
	AlertDialog msg;
	String email, password;
	
	public static int userId = 0;
	
	// TODO - Remember me function
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        msg = new AlertDialog.Builder(this).create();

        // EVENT HANDLING FOR BUTTON LOGIN
    	mLogin = (Button) findViewById(R.id.buttonLogin);
    	mLogin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				login();
			}
		});
    	
    	// EVENT HANDLING FOR BUTTON CREATE NEW ACCOUNT
    	Button register = (Button) findViewById(R.id.buttonLoginCreateNew);
    	register.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent registerIntent = new Intent(v.getContext(), RegisterActivity.class);
				startActivityForResult(registerIntent, 1);
			}
		});
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    
		if (requestCode == 1) { // REGISTER ACTIVITY
			if (resultCode == 0) {
			}
			else if (resultCode == 1) { // Error no connection
				//String id = data.getStringExtra("id");
				msg.setTitle("Error!");
				msg.setMessage(getString(R.string.errorLoginNoConnection));
				msg.show();
			}
			else if (resultCode == 2) { // Error id=0
				//String id = data.getStringExtra("id");
				msg.setTitle("Error!");
				msg.setMessage("Can't create new account!");
				msg.show();
			}
			else if (resultCode == 3) { // SUCCESS 
				String id = data.getStringExtra("id");
				String email = data.getStringExtra("email");
				String pass = data.getStringExtra("password");
				
				EditText mEmail = (EditText) findViewById(R.id.editTextLoginEmail);
				EditText mPass = (EditText) findViewById(R.id.editTextLoginPassword);
				
				mEmail.setText(email);
				mPass.setText(pass);
				userId = Integer.parseInt(id);
			
				login();
			}
		}
	}
	
	private void login() {
		String id="-1";
		mEmail = (EditText) findViewById(R.id.editTextLoginEmail);
		mPassword = (EditText) findViewById(R.id.editTextLoginPassword);
		
		email = mEmail.getText().toString();
		password = mPassword.getText().toString();
		
		// TODO - PASSWORD MUST BE SHA1 HASH
		
		if (email.isEmpty() || password.isEmpty()) {
			msg.setTitle("Error!");
			msg.setMessage("Email and/or password are empty!");
			msg.show();
			return;
		}
	
		client = new RestClient(getString(R.string.serverAddress) + getString(R.string.serverMethodLogin));
		client.AddParam("email", email);
		client.AddParam("password", password);
		
		try {
			client.Execute(RequestMethod.GET);
		} catch (Exception e) {
				e.printStackTrace();
		}
		
		try { 
			id = client.getResponse(); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		}
		
		if (id == null) {
			msg.setTitle("Error!");
			msg.setMessage(getString(R.string.errorLoginNoConnection));
			msg.show();
		}
		else {
			id = id.substring(0, id.length()-1);
			int numId = Integer.parseInt(id);
			if (numId <= 0) {
				msg.setTitle("Error!");
				msg.setMessage(getString(R.string.errorLoginUserOrPass));
				msg.show();
			}
			else {
				Intent mainIntent = new Intent(this, HomeActivity.class);
				startActivity(mainIntent);
			}	
		}
	}
	
}